package com.sp.scmdb.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.SecurityTrainingBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.EmployeeDao;
import com.sp.scmdb.dao.SecurityTrainingDao;
import com.sp.scmdb.dao.SecurityTrainingTypeDao;
import com.sp.scmdb.entity.Employee;
import com.sp.scmdb.entity.SecurityTraining;
import com.sp.scmdb.entity.SecurityTrainingType;
import com.sp.scmdb.service.SecurityTrainingService;

@Service("securityTrainingService")
public class SecurityTrainingServiceImpl implements SecurityTrainingService {
	
	private SecurityTrainingDao securityTrainingDao;
	private SecurityTrainingTypeDao securityTrainingTypeDao;
	private EmployeeDao employeeDao;
	
	public void setSecurityTrainingDao(SecurityTrainingDao securityTrainingDao) {
		this.securityTrainingDao = securityTrainingDao;
	}

	public void setSecurityTrainingTypeDao(SecurityTrainingTypeDao securityTrainingTypeDao) {
		this.securityTrainingTypeDao = securityTrainingTypeDao;
	}

	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	@Override
	@Transactional
	public String createSecurityTraining(SecurityTrainingBean bean) {
		String result = "2";
		SecurityTraining training = new SecurityTraining();
		training.setTitle(bean.getTitle());
		training.setTrainingTypeIds(bean.getTrainingTypeIds());
		training.setTrainDate(bean.getTrainDate());
		training.setTrainMans(bean.getTrainMans());
		training.setCreatedDate(bean.getCreatedDate());
		training.setUser(bean.getUser());
		
		securityTrainingDao.save(training);
		result = CommonParamter.CHECK_SUCCESS;
		
		return result;
	}

	@Override
	@Transactional
	public String delSecurityTraining(String[] arr) {
		String result = "2";
		SecurityTraining securityTraining = null;
		for (int i = 0; i < arr.length; i++) {
			securityTraining = securityTrainingDao.getBySysId(arr[i]);
			securityTrainingDao.delete(securityTraining);
		}
		result = CommonParamter.CHECK_SUCCESS;
		return result;
	}

	@Override
	@Transactional
	public String editSecurityTraining(SecurityTrainingBean bean) {
		String result = "2";
		SecurityTraining training = null;
		training = securityTrainingDao.getBySysId(bean.getSysId());
		training.setTitle(bean.getTitle());
		training.setTrainingTypeIds(bean.getTrainingTypeIds());
		training.setTrainDate(bean.getTrainDate());
		training.setTrainMans(bean.getTrainMans());
		training.setUser(bean.getUser());
		
		securityTrainingDao.update(training);
		result = CommonParamter.CHECK_SUCCESS;
		
		return result;
	}

	@Override
	public List<SecurityTrainingBean> getListSecurityTraining(String hql) {
		SecurityTrainingBean securityTrainingBean = null;
		SecurityTraining securityTraining = null;
		SecurityTrainingType securityTrainingType = null;
		String _hql = "from SecurityTraining where 1 = 1";
		List<SecurityTraining> list = securityTrainingDao.queryHql(_hql);
		List<SecurityTrainingBean> securityTrainingList = new ArrayList<SecurityTrainingBean>();
		
		try {
			for(int i = 0; i < list.size(); i ++) {
				securityTraining = list.get(i);				
				securityTrainingBean = new SecurityTrainingBean();
				securityTrainingBean.setSysId(securityTraining.getSysId());
				securityTrainingBean.setTitle(securityTraining.getTitle());
				securityTrainingBean.setTrainingTypeIds(securityTraining.getTrainingTypeIds());
				if(StringUtils.isNotEmpty(securityTraining.getTrainingTypeIds())) {
					String[] typeIds = securityTraining.getTrainingTypeIds().split(",");
					String typeNames = "";
					for(int j = 0; j < typeIds.length; j++) {
						securityTrainingType = securityTrainingTypeDao.getBySysId(typeIds[j].trim());
						typeNames += securityTrainingType.getTrainTypeName() + ",";
					}
					if(StringUtils.isNotEmpty(typeNames)) {
						typeNames = typeNames.substring(0, typeNames.length() - 1);
					}
					securityTrainingBean.setTrainingTypeNames(typeNames);
				}
					
				securityTrainingBean.setTrainDate(securityTraining.getTrainDate());
				securityTrainingBean.setTrainMans(securityTraining.getTrainMans());
//				if(StringUtils.isNotEmpty(securityTraining.getTrainMans())) {
//					String[] trainMans = securityTraining.getTrainMans().split(",");
//					String trainMansName = "";
//					for(int j = 0; j < trainMans.length; j++) {
//						employee = employeeDao.getById(Integer.parseInt(trainMans[j]));
//						trainMansName += employee.getEmpName() + ",";
//					}
//					if(StringUtils.isNotEmpty(trainMansName)) {
//						trainMansName = trainMansName.substring(0, trainMansName.length() - 1);
//					}
//					securityTrainingBean.setTrainMansName(trainMansName);
//				}
				
				securityTrainingBean.setCreatedDate(securityTraining.getCreatedDate());
				securityTrainingBean.setUser(securityTraining.getUser());
				
				securityTrainingList.add(securityTrainingBean);
			}
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return securityTrainingList;
	}

	@Override
	public SecurityTrainingBean getSecurityTraining(String sysId) {
		SecurityTraining securityTraining = securityTrainingDao.getBySysId(sysId);
		SecurityTrainingBean securityTrainingBean = null;
		Employee employee = null;
		try {
			if(securityTraining != null) {
				securityTrainingBean = new SecurityTrainingBean();
				securityTrainingBean.setSysId(securityTraining.getSysId());
				securityTrainingBean.setTitle(securityTraining.getTitle());
				securityTrainingBean.setTrainingTypeIds(securityTraining.getTrainingTypeIds());
				securityTrainingBean.setTrainDate(securityTraining.getTrainDate());
				securityTrainingBean.setTrainMans(securityTraining.getTrainMans());
				if(StringUtils.isNotEmpty(securityTraining.getTrainMans())) {
					String[] trainMans = securityTraining.getTrainMans().split(",");
					String trainMansName = "";
					for(int j = 0; j < trainMans.length; j++) {
						employee = employeeDao.getById(Integer.parseInt(trainMans[j]));
						trainMansName += employee.getEmpName() + ",";
					}
					if(StringUtils.isNotEmpty(trainMansName)) {
						trainMansName = trainMansName.substring(0, trainMansName.length() - 1);
					}
					securityTrainingBean.setTrainMansName(trainMansName);
				}
				
				securityTrainingBean.setCreatedDate(securityTraining.getCreatedDate());
				securityTrainingBean.setUser(securityTraining.getUser());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return securityTrainingBean;
	}

}
